package org.jeecg.modules.flow.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.flow.api.factory.FlowBaseCloudApiFallbackFactory;
import org.jeecg.modules.flow.entity.FlowAudit;
import org.jeecg.modules.flow.query.*;
import org.jeecg.modules.flow.vo.*;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
@FeignClient(value = "jeecg-flow", fallbackFactory = FlowBaseCloudApiFallbackFactory.class)
@Api(tags = "流程微服务接口")
public interface FlowBaseCloudApi {
    /**
     * 启动流程实例
     *
     * 根据流程种类、启动人机构号、核心企业号
     *
     * @param query 启动流程信息封装嘞
     * @return 启动后的流程实例ID
     */
    @PostMapping("/flow/base/start")
    Result<String> startInstanceByProKey(@RequestBody StartInstanceQuery query);

    /**
     * 查询流程实例
     *
     * @param instanceId 流程实例Id
     * @return
     */
    @ApiOperation("查询流程实例")
    @GetMapping("/flow/base/searchInstanceById")
    Result<ProInstanceVo> searchInstanceById(@RequestParam String instanceId);

    /**
     * 根据流程实例ids查询流程实例列表
     *
     * @param instanceIds
     * @return
     */
    @PostMapping("/flow/base/searchInstanceByIds")
    @ApiOperation(value = "查询流程实例列表-根据流程实例ids", notes = "查询流程实例")
    Result<List<ProInstanceVo>> searchInstanceByIds(@RequestBody Set<String> instanceIds);

    /**
     * 查询流程变量
     *
     * @param taskId        任务ID
     * @param variableNames 变量名称，只对还在运行的任务有效
     * @param isHistory     是否是历史任务
     * @return 返回流程遍历集合
     */
    @ApiOperation(value = "根据taskId查看流程变量", notes = "")
    @GetMapping("/flow/base/getVariablesByTaskId")
    public Map<String, Object> getVariablesByTaskId(@RequestParam("taskId") String taskId, @RequestBody List<String> variableNames, @RequestParam Boolean isHistory);

    /**
     * 删除流程实例
     *
     * @param instanceId 流程实例Id
     * @param reason     删除原因
     * @return
     */
    @GetMapping(path = "/flow/base/delInstanceById")
    public Result<Boolean> delInstanceById(@RequestParam String instanceId, @RequestParam String reason);

    /**
     * 查询代理人是当前用户的任务
     *
     * @param assignee 任务办理人ID
     * @return
     */
    @GetMapping(path = "/flow/base/findTaskByAssignee")
    Result<List<TaskVo>> findTaskByAssignee(@RequestParam String assignee);

    /**
     * 废弃，请使用 /flow/base/findTasks
     * 查询用户的所有任务
     * 包括代办任务、候选组任务、角色任务
     *
     * @return 返回当前用户的任务列表
     */
    @PostMapping("/flow/base/findTaskList")
    @Deprecated
    Result<List<TaskVo>> findTaskList(@RequestBody TaskQuery query);

    @GetMapping("/flow/actRuTask/findTasks")
    Result<Page<TaskVo>> findTasks(@SpringQueryMap TaskQuery query,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize);

    /**
     * 提交任务
     * 必须要传得参数: instanceId,taskId,comment
     * 可选择传得参数: variables,revocationType(撤回类型，撤回任务时需要传)
     * 需要指定提交类型：submitType：1同意、2撤回、3终结、4撤回
     *
     * @param query
     * @return
     */
    @PostMapping(path = "/flow/base/submitTask")
    @ApiOperation(value = "提交任务", notes = "提交任务（同意、撤回、终结、撤回）")
    public Result<Boolean> submitTask(@RequestBody SubmitTaskQuery query);

    /**
     * 根据BusinessKey获取流程实例信息
     *
     * @param businessKey
     * @return
     */
    @GetMapping(path = "/flow/base/getInstanceByBusinessKey")
    public ProInstanceVo getInstanceByBusinessKey(@RequestParam String businessKey);

    /**
     * 根据BusinessKey获取流程实实例下当前活动的任务
     *
     * @param businessKey
     * @return
     */
    @GetMapping(path = "/flow/base/getActiveTaskByBusinessKey")
    TaskVo getActiveTaskByBusinessKey(@RequestParam String businessKey);


    /**
     * 查询历史流程实例信息
     *
     * @param query 参数封装
     * @return
     */
    @GetMapping(path = "/flow/actHiProcessInstance/queryHistoricProcessInstances")
    Result<Page<HistoryProcessInstanceVo>> queryHistoricProcessInstances(@SpringQueryMap HistoryInstanceQuery query
            , @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo
            , @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize);

    /**
     * 查询历史流程活动节点信息
     *
     * @param query
     * @return
     */
    @GetMapping(path = "/flow/actHiActInstance/queryHistoricActivityInstance")
    Result<Page<HistoryActivityInstanceVo>> queryHistoricActivityInstance(@SpringQueryMap HistoryActivityQuery query
            , @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo
            , @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize);

    /**
     * 查询历史任务
     *
     * @param query
     * @return
     */
    @PostMapping(path = "/flow/actHiTaskInstance/queryHistoricTaskInstances")
    Result<Page<HistoryTaskInstanceVo>> queryHistoricTaskInstances(@SpringQueryMap HistoryTaskQuery query
            , @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo
            , @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize);

    /**
     * 根据流程实例id查询审核意见列表
     *
     * @param processInstanceId
     * @return
     */
    @GetMapping("flow/audit/queryAuditListByInstanceId")
    public Result<List<FlowAudit>> queryAuditListByInstanceId(@RequestParam String processInstanceId);


    /**
     * 根据流程任务id查询审核意见列表
     */
    @GetMapping("flow/audit/queryAuditByTaskId")
    public Result<FlowAudit> queryAuditByTaskId(@RequestParam String taskId);

    @GetMapping("/flow/base/timeOut")
    public Result<?> timeOut(@RequestParam Long time);

    @PostMapping(value = "/flow/audit/add")
    public Result<?> add(@RequestBody FlowAudit flowAudit);
}
